home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1998 / MacHack 1998.toast / Papers / OS Shell in Java / Facade / DesktopMenuBar.java < prev    next >
Encoding:
Java Source  |  1998-06-17  |  5.5 KB  |  154 lines  |  [TEXT/dosa]

  1. //    DesktopMenuBar.java : this is a Java source code file for the program Facade.
  2. //    Copyright 1998, Andrew S. Downs
  3. //    andrew.downs@tulane.edu
  4. //
  5. //    This source code is distributed as freeware.
  6. //    Just keep this author information in the file.  Enjoy!
  7.  
  8. import java.awt.*;
  9. import java.awt.event.*;
  10. import java.io.*;
  11. import java.util.*;
  12.  
  13. public class DesktopMenuBar extends DesktopComponent implements Serializable/*, MouseListener*/ {
  14.     int activeMenuId;
  15.     Color highlightColor;
  16.     Vector vector;
  17.     
  18.     DesktopMenuBar() {
  19.         super();
  20.         this.setFont( new Font( "Dialog", Font.BOLD, 12 ) );
  21.         this.setHighlightColor( Global.menuBarHighlightColor );
  22.         vector = new Vector();
  23. //        this.addMouseListener( this );
  24. //        this.addMouseMotionListener( this );
  25.     }
  26.  
  27.     public void setActiveMenuId( int i ) {
  28.         this.activeMenuId = i;
  29.     }
  30.  
  31.     public int getActiveMenuId() {
  32.         return this.activeMenuId;
  33.     }
  34.     
  35.     public void setHighlightColor( Color c ) {
  36.         this.highlightColor = c;
  37.     }
  38.  
  39.     public Color getHighlightColor() {
  40.         return this.highlightColor;
  41.     }
  42.     
  43.     public void setVector( Vector v ) {
  44.         this.vector = v;
  45.     }
  46.  
  47.     public Vector getVector() {
  48.         return this.vector;
  49.     }
  50.     
  51.     public void paint( Graphics g ) {
  52.         // Draw menubar.
  53.         g.setColor( Color.white );
  54.         g.fillRect( this.getX(), this.getY(), this.getWidth(), this.getHeight() );    
  55.     }
  56. /*
  57.     public void mouseClicked( MouseEvent e ) {
  58.     }
  59.     
  60.     public void mouseEntered( MouseEvent e ) {
  61.     }
  62.         
  63.     public void mouseExited( MouseEvent e ) {
  64.     }
  65.  
  66.     public void mouseReleased( MouseEvent e ) {
  67.     }
  68.     
  69.     public void mousePressed( MouseEvent e ) {
  70.         // New click means no previous selection.
  71. //        this.activeMenu = -1;
  72. //        this.activeMenuItem = -1;
  73.  
  74.         Graphics g = this.getGraphics();
  75.         FontMetrics theFontMetrics = g.getFontMetrics();
  76.  
  77.         if ( this.contains( e.getX(), e.getY() ) ) {
  78. System.out.println( "Click in menubar." );
  79.  
  80.             // Handle menu selection.
  81.             for ( int i = 0; i < dmb.getVector().size(); i++ ) {
  82.                 // Get menu object.
  83.                 DesktopMenu d = ( DesktopMenu )dmb.getVector().elementAt( i );
  84.  
  85.                 // Determine if we're inside this menu.
  86.                 if ( ( e.getX() >= d.getX() - Global.defaultMenuHSep ) && ( e.getX() <= ( d.getX() + ( d.getLabel() == null ? ( ( DesktopImageMenu )d ).getImage().getWidth( this ) : theFontMetrics.stringWidth( d.getLabel() ) ) + Global.defaultMenuHSep ) ) 
  87.                 && ( e.getY() >= this.getY() ) && ( e.getY() <= this.getMenuBarHeight() ) ) {
  88.                     // Draw menubar highlighting.
  89.                     g.setColor( Color.blue );
  90.     //                g.fillRect( d.getX() - Global.defaultMenuHSep, this.getY(), ( d.getLabel() == null ? d.getImage().getWidth( this ) : theFontMetrics.stringWidth( d.getLabel() ) ) + ( 2 * Global.defaultMenuHSep ), getMenuBarHeight() );
  91.                     activeMenuRect = new Rectangle( d.getX() - Global.defaultMenuHSep, this.getY(), ( d.getLabel() == null ? ( ( DesktopImageMenu )d ).getImage().getWidth( this ) : theFontMetrics.stringWidth( d.getLabel() ) ) + ( 2 * Global.defaultMenuHSep ), getMenuBarHeight() );
  92.                     g.fillRect(    activeMenuRect.x, activeMenuRect.y, activeMenuRect.width, activeMenuRect.height );
  93.  
  94.                     // Draw menu String or Image.
  95.                     g.setColor( Color.black );
  96.                     if ( d.getLabel() != null )
  97.                         g.drawString( d.getLabel(), d.getX(), d.getY() );
  98.                     else
  99.                         g.drawImage( ( ( DesktopImageMenu )d ).getImage(), d.getX(), d.getY(), this );
  100.                     
  101.                     // Get menu item vector.
  102.                     Vector v = d.getVector();
  103.  
  104.                     DesktopMenuItem dmi = ( DesktopMenuItem )v.elementAt( 0 );
  105.                     if ( dmi.getLabel().equals( Global.menuItemEmptyTrash ) ) {
  106.                         DesktopImage di = ( ( DesktopImage )this.vectorImages.elementAt( 1 ) );
  107.                         String path = di.getPath();
  108.                         File f = new File( path, Global.trashDisplayString );
  109.                         if ( f.exists() ) {
  110.                             String array[] = f.list();
  111.                             
  112.                             if ( array == null || array.length == 0 )
  113.                                 dmi.setEnabled( false );
  114.                             else
  115.                                 dmi.setEnabled( true );
  116.                         }
  117.                     }
  118.                     
  119.                     // Draw menu background.
  120.                     g.setColor( dmb.getBackground() );
  121.                     g.fillRect( d.getItemBounds().getBounds().x, d.getItemBounds().getBounds().y, d.getItemBounds().getBounds().width, d.getItemBounds().getBounds().height );
  122.  
  123.                     // Draw menu items.
  124.                     for ( int j = 0; j < v.size(); j++ ) {
  125.                         if ( ( ( DesktopMenuItem )v.elementAt( j ) ).getLabel().equals( Global.menuSep ) ) {
  126.                             g.setColor( Color.lightGray );
  127.                             g.fillRect( d.getItemBounds().getBounds().x, ( ( DesktopMenuItem )v.elementAt( j ) ).getBounds().y + Global.defaultMenuItemVSep, d.getItemBounds().getBounds().width, 2 );
  128.                             continue;
  129.                         }
  130.                         
  131.                         g.setColor( Color.black );
  132.                         if ( !( ( DesktopMenuItem )v.elementAt( j ) ).getEnabled() )
  133.                             g.setColor( Color.lightGray );
  134.                         g.drawString( ( ( DesktopMenuItem )v.elementAt( j ) ).getLabel(), ( ( DesktopMenuItem )v.elementAt( j ) ).getDrawPoint().x, ( ( DesktopMenuItem )v.elementAt( j ) ).getDrawPoint().y );
  135.                     }
  136.  
  137.                     g.setColor( Color.black );
  138.                     g.drawRect( d.getItemBounds().getBounds().x, d.getItemBounds().getBounds().y, d.getItemBounds().getBounds().width, d.getItemBounds().getBounds().height );
  139.                     g.fillRect( d.getItemBounds().getBounds().x + 2, d.getItemBounds().getBounds().y + d.getItemBounds().getBounds().height, d.getItemBounds().getBounds().width, 2 );
  140.                     g.fillRect( d.getItemBounds().getBounds().x + d.getItemBounds().getBounds().width, d.getItemBounds().getBounds().y + 2, 2, d.getItemBounds().getBounds().height );
  141.                     
  142.                     // Set current menu.
  143.                     this.activeMenu = i;
  144.  
  145.                     // Once found, we're done.
  146.                     break;
  147.                 }
  148.             }
  149.         }
  150.     }
  151. */
  152. }
  153.  
  154.